package com.veo.action;


import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.veo.entity.Books;
import com.veo.service.BookService;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component("BooksMsgAction")
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
@Results({
        @Result(name = "success",type = "json",params = {"root","resultMap"}),
})
public class BooksMsgAction extends ActionSupport implements ModelDriven<Books> {

    private Books books=new Books();
    @Override
    public Books getModel() {
        return books;
    }

    private Map<String,String> resultMap;

    public Map<String, String> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, String> resultMap) {
        this.resultMap = resultMap;
    }

    @Autowired
    private BookService bookService;

    @Action("deleteBook")
    public String deleteBook(){
        resultMap=new HashMap<String,String>();
        try {
            bookService.deleteBook(books);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return SUCCESS;
        }
        return SUCCESS;
    }


    @Action("addBook")
    public String addBook(){
        resultMap=new HashMap<String,String>();
        try {
            bookService.addBook(books);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return SUCCESS;
        }
        return SUCCESS;
    }


    @Action("updateBook")
    public String updateBook(){
        resultMap=new HashMap<String,String>();
        try {
            System.out.println(books);
            bookService.updateBook(books);
            resultMap.put("msg","ok");
        }catch (Exception e){
            resultMap.put("msg","error");
            return SUCCESS;
        }
        return SUCCESS;
    }


}
